var geometryService, map, toolbar, symbol, tb;
//var layerweifanghr = new esri.layers.ArcGISDynamicMapServiceLayer("http://122.4.252.56:8399/arcgis/rest/services/hrbuss/MapServer");
//var layerjx = new esri.layers.ArcGISDynamicMapServiceLayer("http://220.175.6.119:8399/arcgis/rest/services/jxxx/MapServer", {"opacity": 0.4});
////var layer999 = new esri.layers.ArcGISDynamicMapServiceLayer("http://192.168.3.225:8399/arcgis/rest/services/999/MapServer");
//var layer = new esri.layers.ArcGISDynamicMapServiceLayer("http://218.200.166.71:10041/ArcGIS/rest/services/guanxian/MapServer", {id: "basemap"});
//var layercky = new esri.layers.ArcGISDynamicMapServiceLayer("http://218.200.166.71:10041/ArcGIS/rest/services/cky/MapServer");
//var myLayeryinchuan = new esri.layers.ArcGISDynamicMapServiceLayer("http://222.75.145.78:8399/arcgis/rest/services/MapIndex/MapServer", {"opacity": 1});
//var myLayerqd = new esri.layers.ArcGISDynamicMapServiceLayer("http://219.146.91.68:8399/arcgis/rest/services/qdtn/MapServer", {"opacity": 0.6});
//var layerny = new esri.layers.ArcGISDynamicMapServiceLayer("https://gis.hnytnet.com:8343/arcgis/rest/services/Electricity/MapServer");
//var layerny1 = new esri.layers.ArcGISDynamicMapServiceLayer("https://gis.hnytnet.com:8343/arcgis/rest/services/thematic/MapServer");
//var layeranhui = new esri.layers.ArcGISDynamicMapServiceLayer("http://61.190.89.176:8399/arcgis/rest/services/gx/MapServer");
//var layercb = new esri.layers.ArcGISDynamicMapServiceLayer("http://202.98.153.248:8399/arcgis/rest/services/guanxian/MapServer");
//var layersx = new esri.layers.ArcGISDynamicMapServiceLayer("http://202.97.132.183:8399/arcgis/rest/services/test5/MapServer");

require([
        "dojo/dom",
        "dijit/registry",
        "dojo/_base/connect",
        "dojo/colors",
        "dojo/_base/array",
        "esri/map",
        // "esri/dijit/Scalebar",
        "esri/dijit/OverviewMap",
        "esri/dijit/Popup",
        "esri/tasks/geometry",
        'esri/toolbars/draw',
        'esri/dijit/Measurement',
        "dojo/domReady!"
    ],
    function(dojo, dijit, connect, Color, dojoArray) {
        var popup = new esri.dijit.Popup(null, document.createElement("div"));
        map = new esri.Map("mapContainer", {
            sliderPosition: "top-left",
            infoWindow: popup,
            displayGraphicsOnPan: true,
            slider: true,
            center: [108.95519667, 34.25689833],
            //center: [119.38667, 36.58233],
            zoom: 11,
            nav: false,
            logo: false
        });
        // var scalebar = new esri.dijit.Scalebar({
        //     map: map
        // });
        //	map.addLayer(new GoogleMapLayer());
        //	map.addLayer(new GoogleMapAnooLayer());

        map.addLayer(new googleimg());
        //	map.addLayer(new gaode());
        //	map.addLayer(new sogouimg());
        map.addLayer(new tiandibiaozhu());
        //	map.addLayer(layer); 
        //	map.addLayer(layerny);    
        //	map.addLayer(layerny1);    
        //	map.addLayer(myLayeryinchuan);
        //	map.addLayer(myLayerqd);
        //	map.addLayer(layerjx);
        //	map.addLayer(layerweifanghr);
        //	map.addLayer(layercb);
        //	map.addLayer(layersx);
        //	map.addLayer(new esri.layers.ArcGISDynamicMapServiceLayer("http://61.190.89.176:8399/arcgis/rest/services/ahxx1/MapServer"));

        connect.connect(map, "onMouseDown", showCoordinates);

        function showCoordinates(event) {
            var mp = event.mapPoint;
            var mp2 = event.screenPoint;
            var lat = mercator2lonlat({
                x: mp.x,
                y: mp.y
            });
            window.parent.document.getElementById("msg").innerHTML = "经度:" + lat.x + "<br>纬度:" + lat.y + "<br>屏幕坐标：" + mp2.x + "," + mp2.y;
            //dojo.byId("msgg").innerHTML = "地理坐标：" + lat.x + "," + lat.y + "&nbsp;屏幕坐标：" + mp2.x + "," + mp2.y
        };
        toolbar = new esri.toolbars.Draw(map);
        //添加toolbar画图完成后事件监听调用doMeasure
        connect.connect(toolbar, "onDrawEnd", doMeasure);
        //实例化GeometryService，地址为arcgis发布的GeometryServer
        //geometryService = new esri.tasks.GeometryService("http://218.200.166.71:10041/ArcGIS/rest/services/Geometry/GeometryServer");
        //lengthParams = new esri.tasks.LengthsParameters();
        //geometryService = new esri.tasks.GeometryService("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
        //添加onProjectComplete完成后事件监听
        //connect.connect(geometryService, "onProjectComplete", projectComplete);

        //量算
        function doMeasure(geometry) {
            //更加类型设置显示样式
            switch (geometry.type) {
                case "polyline":
                    var symbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, 1);
                    break;
                case "polygon":
                    var symbol = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_NONE, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_DASHDOT, new dojo.Color("blue"), 2), new dojo.Color([255, 255, 0, 0.25]));
                    break;
            }
            //设置样式
            var graphic = new esri.Graphic(geometry, symbol);
            //清除上一次的画图内容
            //map.graphics.clear();
            map.graphics.add(graphic);
            //进行投影转换，完成后调用projectComplete
            //geometryService.project([graphic], new esri.SpatialReference({"wkid":32618}));
            projectComplete(graphic);
        }

        //投影转换完成后调用方法
        function projectComplete(graphics) {
            //如果为线类型就进行lengths距离测算
            if (graphics.geometry.type == "polyline") {

                connect.connect(geometryService, "onLengthsComplete", outputDistance);
                var x1 = graphics.geometry.paths[0][0][0];
                var y1 = graphics.geometry.paths[0][0][1];
                var x2 = graphics.geometry.paths[0][1][0];
                var y2 = graphics.geometry.paths[0][1][1];
                //console.info(x1);
                var result = Math.sqrt(Math.abs((x1 - x2) * (x1 - x2) - (y1 - y2) * (y1 - y2)));
                outputDistance(result);
            }
            //如果为面类型需要先进行simplify操作在进行面积测算
            else if (graphics.geometry.type == "polygon") {
                connect.connect(geometryService, "onSimplifyComplete", simplifyComplete);
                geometryService.simplify([graphics]);
            }
        }
        //显示测量距离
        function outputDistance(result) {
            //console.info('这就是要显示的距离'+dojo.number.format(result.lengths[0] / 1000)) ;
            window.parent.document.getElementById("des-msgg").innerHTML = "距离：" + result / 1000 + "千米";
        }
        ///////////////////////////////////////////////////
        geometryService = new esri.tasks.GeometryService("http://sampleserver1.arcgisonline.com/arcgis/rest/services/Geometry/GeometryServer");
        esriConfig.defaults.io.proxyUrl = "/proxy";
        esriConfig.defaults.io.alwaysUseProxy = false;
        tb = new esri.toolbars.Draw(map);
        connect.connect(tb, "onDrawEnd", doDraw);
    });
//测量距离
function mhgis_distance() {
    toolbar.activate(esri.toolbars.Draw.LINE);
};

function cky() {
    var type = window.parent.Ext.getCmp('center_panel_button_cky').getText();
    if (type == '显示穿跨越') {
        window.parent.Ext.getCmp('center_panel_button_cky').setText('隐藏穿跨越');
        map.addLayer(layercky);
    } else {
        window.parent.Ext.getCmp('center_panel_button_cky').setText('显示穿跨越');
        map.removeLayer(layercky);
    }
};

function switchmap() {
    //var type = window.parent.document.getElementById('switchmap').innerHTML;
    var type = window.parent.Ext.getCmp('center_panel_button_switchmap').getText();
    //console.info(type);
    if (type == '地形') {
        //document.getElementById('switchmap').innerHTML = '影像';
        window.parent.Ext.getCmp('center_panel_button_switchmap').setText('影像');
        window.parent.Ext.getCmp('center_panel_button_cky').setText('显示穿跨越');
        map.removeAllLayers();
        map.addLayer(new tiandidixing());
        map.addLayer(new tiandibiaozhu());
        map.addLayer(layer);
        //map.addLayer(layercky);
    } else {
        //document.getElementById('switchmap').innerHTML = '地形';
        window.parent.Ext.getCmp('center_panel_button_switchmap').setText('地形');
        window.parent.Ext.getCmp('center_panel_button_cky').setText('显示穿跨越');
        map.removeAllLayers();
        map.addLayer(new googleimg());
        map.addLayer(new tiandiimg());
        map.addLayer(new tiandibiaozhu());
        map.addLayer(layer);
        //map.addLayer(layercky);
    }
};

function googlemap() {
    //window.parent.Ext.getCmp('center_panel_button_cky').setText('显示穿跨越');
    map.removeAllLayers();
    map.addLayer(new googleimg());
    map.addLayer(new tiandibiaozhu());
    map.addLayer(layer);
    map.addLayer(layerweifanghr);
    //map.addLayer(layercky);
    //map.addLayer(myLayeryinchuan);    
    //map.addLayer(myLayerqd)
};

function qqmap() {
    //window.parent.Ext.getCmp('center_panel_button_cky').setText('显示穿跨越');
    map.removeAllLayers();
    map.addLayer(new QQLayer());
    map.addLayer(new tiandibiaozhu());
    map.addLayer(layer);
    map.addLayer(layerweifanghr);
    //map.addLayer(layercky);
    //map.addLayer(myLayeryinchuan);
    //map.addLayer(myLayerqd)
};

function gaodemap() {
    //window.parent.Ext.getCmp('center_panel_button_cky').setText('显示穿跨越');
    map.removeAllLayers();
    map.addLayer(new gaode());
    map.addLayer(new tiandibiaozhu());
    map.addLayer(layer);
    map.addLayer(layerweifanghr);
    //map.addLayer(layercky);
    //map.addLayer(myLayeryinchuan);
    //map.addLayer(myLayerqd)
};

function arcgismap() {
    //window.parent.Ext.getCmp('center_panel_button_cky').setText('显示穿跨越');
    map.removeAllLayers();
    map.addLayer(new arcgisimg());
    map.addLayer(new tiandibiaozhu());
    map.addLayer(layer);
    map.addLayer(layerweifanghr);
    //map.addLayer(layercky);
    //map.addLayer(myLayeryinchuan);
    //map.addLayer(myLayerqd)
};

function bingmap() {
    //window.parent.Ext.getCmp('center_panel_button_cky').setText('显示穿跨越');
    map.removeAllLayers();
    veTileLayer = new esri.virtualearth.VETiledLayer({
        bingMapsKey: 'Ai1gq1yTN5JtOcAP6rc1627t3rXtWdOWKi9tkKvonfhGaZEVgGkELopbVTXGVEF0',
        mapStyle: esri.virtualearth.VETiledLayer.MAP_STYLE_AERIAL
    });
    map.addLayer(veTileLayer);
    map.addLayer(new tiandibiaozhu());
    map.addLayer(layer);
    map.addLayer(layerweifanghr);
    //map.addLayer(layercky);
    //map.addLayer(myLayeryinchuan);
    //map.addLayer(myLayerqd)
};

function getLabelSymbol(labelName) {
    var width = (labelName.length + 1) * 9 + 7;
    labelName = encodeURIComponent(labelName);
    var url = "mapHelp/getLabelImage/" + labelName + "/" + width;
    var symbol = new esri.symbol.PictureMarkerSymbol(url, width, 18);
    symbol.setOffset(width / 2 + 8, 0);
    return symbol
};

function addGraphic(geometry, symbol) {
    return map.graphics.add(new esri.Graphic(geometry, symbol))
};
var template = {
    "checkData": null,
    "event": null
};
var inspectAnimalConfig = {
    mapPoints: [],
    polyLineG: null,
    countLength: null,
    speed: 2000,
    animation: null,
    playPolyLine: null
};
var symbol = {
    //"checkData": new esri.symbol.PictureMarkerSymbol("../../../../PUBLIC/images/markerGreen.png", 30, 30),
    "checkData": new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color('#0000ff'), 1), new dojo.Color('#ffff40')),
    "event": new esri.symbol.PictureMarkerSymbol(TMPL + '/Weigis/arcgis_js_api/markerRed.png', 30, 30),
    "keypoint": new esri.symbol.PictureMarkerSymbol(TMPL + '/Weigis/arcgis_js_api/keypoint.png', 30, 30),
    "line": new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color("#00ff00"), 3)
};

function addKeypointOnlyGraphics(data, symbolType) {
    var result = JSON.parse(JSON.stringify(data));
    if (map.graphics) {
        //map.graphics.clear()
    };
    if (result.x < -179 || result.x > 179 || result.y < -89 || result.y > 89 || result.x == '' || null == result.x || result.y == '' || null == result.y) {
        alert('经纬度超出正常范围！');
        return;
    }
    var p = {
        x: parseFloat(result.x),
        y: parseFloat(result.y)
    };
    var mercator = lonlat2mercator(p);
    result.x = mercator.x;
    result.y = mercator.y;
    //////////////////////////////////////
    map.infoWindow.hide();
    var point = new esri.geometry.Point(Number(result.x), Number(result.y), map.spatialReference);
    var infoTemplate = new esri.InfoTemplate("采集时间：" + result.addtime, "机器编号:" + result.weiuser_openid + "<br/>巡线工:" + result.weiuser.nickname + "<br/>关键点名称:" + result.title + "<br/>备注:" + result.remark + "<br/>经度:" + result.lng + "<br/>纬度:" + result.lat + "<br/>地址:" + result.address + "<br/>照片:<img style='width:220px;' src=" + ROOT + "/Wll/Tpl/Res/upload/" + result.img + ">");
    var g = new esri.Graphic(point, symbol['keypoint'], result, infoTemplate);
    map.graphics.add(g);
    map.centerAndZoom(point);
};

function addEventOnlyGraphics(data, symbolType) {
    var result = JSON.parse(JSON.stringify(data));
    if (map.graphics) {
        //map.graphics.clear()
    };
    //alert(result.X+''+result.Y);
    if (result.x < -179 || result.x > 179 || result.y < -89 || result.y > 89 || result.x == '' || null == result.x || result.y == '' || null == result.y) {
        alert('经纬度超出正常范围！' + result.x + '-' + result.y);
        return;
    }
    //	return false;
    var p = {
        x: parseFloat(result.x),
        y: parseFloat(result.y)
    };
    var mercator = lonlat2mercator(p);
    result.x = mercator.x;
    result.y = mercator.y;
    //////////////////////////////////////
    map.infoWindow.hide();
    var point = new esri.geometry.Point(Number(result.x), Number(result.y), map.spatialReference);
    var infoTemplate = new esri.InfoTemplate("采集时间：" + result.addtime, "机器编号:" + result.weiuser_openid + "<br/>巡线工:" + result.weiuser.nickname + "<br/>事件详情:" + result.type + "<br/>备注:" + result.remark + "<br/>经度:" + result.lng + "<br/>纬度:" + result.lat + "<br/>地址:" + result.address + "<br/>照片:<img style='width:220px;' src=" + ROOT + "/Wll/Tpl/Res/upload/" + result.img + ">");
    var g = new esri.Graphic(point, symbol['event'], result, infoTemplate);
    map.graphics.add(g);
    map.centerAndZoom(point);
};

//显示所有关键点
function addGraphicsKeypoint(result, symbolType) {
    $.each(result, function(i, n) {
        var p = {
            x: parseFloat(n.x),
            y: parseFloat(n.y)
        };
        var mercator = lonlat2mercator(p);
        result[i].x = mercator.x;
        result[i].y = mercator.y
    });
    var template = {
        "checkData": null,
        "event": null
    };
    if (map.graphics) {
        //map.graphics.clear()
    };
    if (result == null || typeof(result) == "undefined" || result.length < 1) {
        top.index_nfk("没有查询数据！");
        return
    };
    ///////////////////////////////////////给巡线点旁边加名称
    /*dojo.forEach(result, function(item,i){
    	var textSymbol = new esri.symbol.TextSymbol({
    		text: item.checktime+'-'+item.name,
    		font: new esri.symbol.Font("12px", esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL, esri.symbol.Font.WEIGHT_BOLDER),
    		yoffset:-17,xoffset:5,
    		color:new dojo.Color("red")
    	});
    	this.labelG= new esri.Graphic(new esri.geometry.Point(item.x, item.y, map.spatialReference), textSymbol);
    	map.graphics.add(this.labelG);
    });*/
    ///////////////////////////////////////////////////////////////////////////
    map.infoWindow.hide();
    var multipoint = new esri.geometry.Multipoint(map.spatialReference);
    dojo.forEach(result, function(item, i) {
        var point = new esri.geometry.Point(Number(item.x), Number(item.y), map.spatialReference);
        multipoint.addPoint(point);
        var infoTemplate = new esri.InfoTemplate("采集时间：" + item.checktime, "机器编号:" + item.machinecode + "<br/>关键点:" + item.name + "<br/>类型:" + item.type + "<br/>备注:" + item.remark + "<br/>经度:" + item.y + "<br/>纬度:" + item.x + "<br/>照片:<img style='width:220px;' src=" + item.photo + ">");
        var g = new esri.Graphic(point, symbol['keypoint'], item, infoTemplate);
        map.graphics.add(g);
    });
    map.setExtent(multipoint.getExtent(), true);
};

//显示巡线轨迹
var ptcount = null;

function addGraphics(result, symbolType) {
    //	console.info(result);
    ptcount = result;
    $.each(result, function(i, n) {
        var p = {
            x: parseFloat(n.x),
            y: parseFloat(n.y)
        };
        var mercator = lonlat2mercator(p);
        result[i].x = mercator.x;
        result[i].y = mercator.y
    });
    var template = {
        "checkData": null,
        "event": null
    };
    if (map.graphics) {
        map.graphics.clear()
    };
    if (result == null || typeof(result) == "undefined" || result.length < 1) {
        top.index_nfk("没有查询数据！");
        return
    };
    map.infoWindow.hide();
    //////////////////////////////////////////巡线点连线
    var inputJson = {
        paths: [
            []
        ],
        spatialReference: map.spatialReference
    };
    //根据查询结果,组装折线路径
    dojo.forEach(result, function(result) {
        var x = Number(result.x);
        var y = Number(result.y);
        inputJson.paths[0].push([x, y]);
    });
    var polyLine = new esri.geometry.Polyline(inputJson);
    map.graphics.add(new esri.Graphic(polyLine, symbol.line));
    ////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////给巡线点旁边加时间
    /*dojo.forEach(result, function(result,i){
    	var textSymbol = new esri.symbol.TextSymbol({
    		text: result.checktime,
    		font: new esri.symbol.Font("15px", esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL, esri.symbol.Font.WEIGHT_BOLDER),
    		yoffset:-2,xoffset:75,
    		color:new dojo.Color("ffff40")
    	});
    	this.labelG= new esri.Graphic(new esri.geometry.Point(result.x, result.y, map.spatialReference), textSymbol);
    	map.graphics.add(this.labelG);
    });*/
    ///////////////////////////////////////////////////////////////////////////
    map.infoWindow.hide();
    var multipoint = new esri.geometry.Multipoint(map.spatialReference);
    dojo.forEach(result, function(item, i) {
        var point = new esri.geometry.Point(Number(item.x), Number(item.y), map.spatialReference);
        multipoint.addPoint(point);
        var infoTemplate = new esri.InfoTemplate("采集时间：" + item.checktime, "机器编号:" + item.weiuser_openid + "<br/>经度:" + item.lng + "<br/>纬度:" + item.lat + "<br/>地址:" + item.address);
        var g = new esri.Graphic(point, symbol['checkData'], item, infoTemplate);
        map.graphics.add(g);
    });
    map.setExtent(multipoint.getExtent(), true);
};

//点击查询后显示所有的巡检点
function addGraphicsAll(result, symbolType) {
    ptcount = result;
    //console.info(ptcount);
    $.each(result, function(i, n) {
        var p = {
            x: parseFloat(n.x),
            y: parseFloat(n.y)
        };
        var mercator = lonlat2mercator(p);
        result[i].x = mercator.x;
        result[i].y = mercator.y
    });
    var template = {
        "checkData": null,
        "event": null
    };
    if (map.graphics) {
        map.graphics.clear()
    };
    if (result == null || typeof(result) == "undefined" || result.length < 1) {
        top.index_nfk("没有查询数据！");
        return
    };
    map.infoWindow.hide();
    //////////////////////////////////////////巡线点连线
    /*var inputJson = {
    	paths: [[]],
    	spatialReference: map.spatialReference
    };
    //根据查询结果,组装折线路径
    dojo.forEach(result, function(result){
    	var x = Number(result.x);
    	var y = Number(result.y);
    	inputJson.paths[0].push([x, y]);
    });
    var polyLine = new esri.geometry.Polyline(inputJson);
    map.graphics.add(new esri.Graphic(polyLine,symbol.line));*/
    ////////////////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////给巡线点旁边加时间
    /*dojo.forEach(result, function(result,i){
    	var textSymbol = new esri.symbol.TextSymbol({
    		text: result.checktime,
    		font: new esri.symbol.Font("15px", esri.symbol.Font.STYLE_NORMAL, esri.symbol.Font.VARIANT_NORMAL, esri.symbol.Font.WEIGHT_BOLDER),
    		yoffset:-2,xoffset:75,
    		color:new dojo.Color("ffff40")
    	});
    	this.labelG= new esri.Graphic(new esri.geometry.Point(result.x, result.y, map.spatialReference), textSymbol);
    	map.graphics.add(this.labelG);
    });*/
    ///////////////////////////////////////////////////////////////////////////
    map.infoWindow.hide();
    var multipoint = new esri.geometry.Multipoint(map.spatialReference);
    dojo.forEach(result, function(item, i) {
        var point = new esri.geometry.Point(Number(item.x), Number(item.y), map.spatialReference);
        multipoint.addPoint(point);
        var infoTemplate = new esri.InfoTemplate("采集时间：" + item.checktime, "机器编号:" + item.machinecode + "<br/>巡线工:" + item.name + "<br/>经度:" + item.y + "<br/>纬度:" + item.x + "<br/>备注:" + item.remark + "<br/>落入关键点名称:" + item.keypointid + "<br/>地址:" + item.bdaddress);
        var g = new esri.Graphic(point, symbol['checkData'], item, infoTemplate);
        map.graphics.add(g);
    });
    map.setExtent(multipoint.getExtent(), true);
};
//////////////////////////////////////////轨迹回放
var list, timer;
var defaultSymbol;
var icount;
var i = 0;
var graphic_guiji = new Array();

function ptcounntSplit() {
    if (ptcount == null) return false;
    icount = ptcount.length;
}

function go() {
    if (i > 0) {
        map.graphics.remove(graphic_guiji[i - 1]);
    }
    var pointSymbol = new esri.symbol.PictureMarkerSymbol(TMPL + '/Weigis/arcgis_js_api/sogoumarker.gif', 18, 37);
    var geometry = new esri.geometry.Point(ptcount[i].x, ptcount[i].y, map.spatialReference);
    graphic_guiji[i] = new esri.Graphic(geometry, pointSymbol);
    map.graphics.add(graphic_guiji[i]);
    var extent = map.extent;
    map.centerAndZoom(geometry, 16);
    i++;
    //console.info(i+''+icount);
    if (i == (icount)) {
        clearInterval(timer);
        window.parent.center_panel_button_huifang.setText('开始轨迹回放');
    }
}

function stop() {
    //console.info('停止');
    clearInterval(timer);
}

function gogps() {
    if (ptcount == null) return false;
    for (var m = 0; m <= i; m++) {
        map.graphics.remove(graphic_guiji[m]);
    }
    i = 0;
    timer = setInterval("go()", "1000");
}

//////////////////////////////////////////////轨迹回放结束
function playAnimation() {
    if (inspectAnimalConfig.animation != null) {
        inspectAnimalConfig.animation.play()
    }
};

function pauseAnimation() {
    if (inspectAnimalConfig.animation != null) {
        inspectAnimalConfig.animation.pause()
    }
};

function createAnimation(i, onEndFun) {
    var mapPoints = inspectAnimalConfig.mapPoints;
    if (typeof(i) == "undefined" || i < 0) {
        i = 0
    };
    if (mapPoints.length < 1 || (i + 1) == mapPoints.length) {
        return
    };
    var countLength = inspectAnimalConfig.countLength;
    if (null == countLength) {
        var inspectPolyline = new esri.geometry.Polyline(map.spatialReference);
        inspectPolyline.addPath(mapPoints);
        countLength = esri.geometry.geodesicLengths([inspectPolyline], esri.Units.METERS)[0];
        inspectAnimalConfig.countLength = countLength
    };
    var polyLineG = inspectAnimalConfig.polyLineG;
    var lastMapPoint = mapPoints[i];
    var tempPolyline = new esri.geometry.Polyline(map.spatialReference);
    tempPolyline.addPath([lastMapPoint]);
    if (null == polyLineG) {
        if (inspectAnimalConfig.playPolyLine == null) {
            inspectAnimalConfig.playPolyLine = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([102, 0, 51]), 4)
        };
        polyLineG = map.graphics.add(new esri.Graphic(dojo.clone(tempPolyline), inspectAnimalConfig.playPolyLine));
        inspectAnimalConfig.polyLineG = polyLineG
    };
    var nowMapPoint = mapPoints[i + 1];
    tempPolyline.insertPoint(0, 1, nowMapPoint);
    var tempLenth = esri.geometry.geodesicLengths([tempPolyline], esri.Units.METERS)[0];
    var duration = (tempLenth / countLength * inspectAnimalConfig.speed);
    var coorLength = esri.geometry.getLength(lastMapPoint, nowMapPoint);
    var x1 = lastMapPoint.x;
    var y1 = lastMapPoint.y;
    var x2 = nowMapPoint.x;
    var y2 = nowMapPoint.y;
    var cos = 0;
    var sin = 0;
    if (coorLength != 0) {
        cos = (x2 - x1) / coorLength;
        sin = (y2 - y1) / coorLength
    };
    var animation = new dojo.Animation({
        duration: duration,
        curve: [0, coorLength],
        onAnimate: function(step) {
            if (isNaN(step)) {
                step = coorLength
            };
            x2 = x1 + cos * step;
            y2 = y1 + sin * step;
            polyLineG.geometry.paths[0].push([x2, y2]);
            map.graphics.refresh()
        },
        onEnd: function() {
            if (i < mapPoints.length - 2) {
                createAnimation(i + 1, onEndFun).play()
            } else {
                onEndFun();
                map.graphics.remove(polyLineG);
                inspectAnimalConfig.polyLineG = null;
                createAnimation(0, onEndFun)
            }
        }
    });
    inspectAnimalConfig.animation = animation;
    return animation
};